<?php

    require_once Core_IO_Path::combine(SERUM_MODULE_PATH, 'Base', 'controllers', 'front', 'IndexController.php');

    /**
     * @package      Modules
     * @subpackage   Pages_Controllers_Cron
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
     class Pages_Controllers_Cron_Index extends Core_Controller_Action_Abstract
     {

    ////////////////////////////////////////////////////////////////////////////
    //                                Variables                               //
    ////////////////////////////////////////////////////////////////////////////

       /**
        * Parameters for plugin
        *
        * @var array
        */
        protected $_pluginParams = array('*' => array('auth.required' => false));

    ////////////////////////////////////////////////////////////////////////////
    //                                Public methods                          //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Gets models list for google adsense
         *
         * @return  Core_Controller_Response_Abstract
         */
        public function siteMap()
        {
            $rep = $this->_getResponse('http');
            $rep->setTemplate('main.tpl');

            // One week time limit ! :-O
            set_time_limit(3600 * 24 * 7);

            $siteMapDir = Core_IO_Path::combine(PROJECT_WWW_PATH, 'sitemap');

            if (!Core_IO_Directory::exists($siteMapDir)) {
                Core_IO_Directory::createDirectory($siteMapDir);
            }

            $brands = Doctrine_Query::create()
                        ->select('b.id, b.subdomain')
                        ->from('DbTecdocbrand b')
                        ->where('b.status = ?', DbBase::ENABLED)
                        ->execute(array(), Doctrine::HYDRATE_ARRAY);

            foreach ($brands as $brand) {
                $parser = new Core_Parser_Sitemap();

                $models = Doctrine_Query::create()
                        ->select('m.id, m.slug, m.last_modification_date')
                        ->from('DbTecdocmodel m')
                        ->where('m.status = ?', DbBase::ENABLED)
                        ->addWhere('m.brandId = ?', $brand['id'])
                        ->orderBy('m.last_modification_date DESC')
                        ->execute(array(), Doctrine::HYDRATE_ARRAY);

                foreach ($models as $model) {
                    $date = new Zend_Date($model['last_modification_date']);

                    $search = array('brand'     => $brand['subdomain'],
                                    'model'     => $model['slug']);

                    $url = Base_Controllers_Front_Index::getNavigationUrl($search);

                    $parser->addUrl($url, $date->get('YYYY-MM-dd'), null, null);

                    $vehicles = Doctrine_Query::create()
                                    ->select('v.id, v.slug, v.last_modification_date')
                                    ->from('DbTecdocvehicle v')
                                    ->where('v.status = ?', DbBase::ENABLED)
                                    ->addWhere('v.modelId = ?', $model['id'])
                                    ->orderBy('v.last_modification_date DESC')
                                    ->execute(array(), Doctrine::HYDRATE_ARRAY);

                    foreach ($vehicles as $vehicle) {
                        $date = new Zend_Date($vehicle['last_modification_date']);

                        $search = array('brand'     => $brand['subdomain'],
                                        'model'     => $model['slug'],
                                        'vehicle'   => $vehicle['slug']);

                        $url = Base_Controllers_Front_Index::getNavigationUrl($search);

                        $parser->addUrl($url, $date->get('YYYY-MM-dd'), null, null);
                    }

                    unset($vehicles);
                }

                unset($models);

                /*
                 * Writes file
                 */
                $parser->write(Core_IO_Path::combine($siteMapDir, 'sitemap-'. $brand['subdomain'] .'.xml'));
            }

            return $rep;
        }

    }
